package com.sunshine.wrongquestionset.activity.exercise_history

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import com.sunshine.wrongquestionset.room.entity.*
import com.sunshine.wrongquestionset.room.repository.MyRepository

/**
 * @author sunshine
 * @date 2021/4/13
 */
class ExerciseHistoryViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: MyRepository = MyRepository(application.applicationContext)

    /**
     * 获取所有可以选择的错题集
     */
    fun getQuestionSet(userId: String, subjectId: Int): LiveData<List<QuestionSetEntity>?> {
        return repository.getQuestionSetBySubjectId(userId, subjectId)
    }

    /**
     * 获取所有可以选择的科目
     */
    fun getQuestionSubject(userId: String): LiveData<List<SubjectEntity>?> {
        return repository.getAllSubject(userId)
    }

    fun getQuestionInSubject(subjectId: Int): LiveData<List<QuestionEntity>?> {
        return repository.getQuestionBySubject(subjectId)
    }

    fun getQuestionInSet(setId: Int): LiveData<List<QuestionEntity>?> {
        return repository.getAllQuestionInSetId(setId)
    }

    fun getQuestion(userId: String): LiveData<List<QuestionEntity>?> {
        return repository.getUserAllQuestion(userId)
    }

    /**
     * 获取练习记录
     */
    fun getExerciseHistory(questionId: Int): LiveData<List<WrongQuestionEntity>?> {
        return repository.getWrongQuestionByQuestion(questionId)
    }

    fun getExerciseHistoryInDate(startTime: Long, endTime: Long): LiveData<List<WrongQuestionEntity>?> {
        return repository.getWrongQuestionInDate(startTime, endTime)
    }

    fun getExerciseHistoryInDateAndQuestionId(questionId: Int, startTime: Long, endTime: Long): LiveData<List<WrongQuestionEntity>?> {
        return repository.getWrongQuestionInDateAndQuestion(questionId, startTime, endTime)
    }

    /**
     * 查询指定错题集指定日期的错题
     */
    private fun getWrongQuestionInSetAndInDate(setId: Int, startTime: Long, endTime: Long): LiveData<List<WrongQuestionBean>?> {
        return repository.getWrongQuestionBeanInSetIdAndInDate(setId, startTime, endTime)
    }

    /**
     * 查询指定科目指定日期的错题
     */
    private fun getWrongQuestionInSubjectAndInDate(subjectId: Int, startTime: Long, endTime: Long): LiveData<List<WrongQuestionBean>?> {
        return repository.getWrongQuestionBeanInSubjectIdAndInDate(subjectId, startTime, endTime)
    }

    /**
     * 查询指定日期的错题
     */
    fun getWrongQuestionInDate(subjectId: Int, setId: Int, startTime: Long, endTime: Long): LiveData<List<WrongQuestionBean>?> {
        //筛选科目
        return if (subjectId != -1) {
            //筛选错题集
            if (setId != -1) {
                getWrongQuestionInSetAndInDate(setId, startTime, endTime)
            }
            //该科目全部错题集
            else {
                getWrongQuestionInSubjectAndInDate(subjectId, startTime, endTime)
            }

        }
        //全部科目
        else {
            repository.getWrongQuestionBeanInDate(startTime, endTime)
        }
    }
}